<?php


namespace Modules\Common\General\Security\Authorization\Concerns;


use Illuminate\Support\Facades\DB;
use Modules\Common\General\Security\Authorization\Casbin;

trait ManageRoleUser
{

    public function addRolesForUser(int $userId, array $roleIds)
    {
        DB::transaction(function () use ($userId, $roleIds) {
            foreach ($roleIds as $roleId) {
                Casbin::addRoleForUser("u$userId", "r$roleId");
            }
        });
    }

    public function deleteRolesForUser(int $userId, array $roleIds)
    {
        DB::transaction(function () use ($userId, $roleIds) {
            foreach ($roleIds as $roleId) {
                Casbin::deleteRoleForUser("u$userId", "r$roleId");
            }
        });
    }

    public function getUsersForRole(int $roleId)
    {
        return Casbin::getUsersForRole("r$roleId");
    }


    public function getRolesForUser(int $userId)
    {
        return Casbin::getRolesForUser("u$userId");
    }

    public function getRolesForUserForPresentation(int $userId)
    {
        return $this->rolesForPresentation(Casbin::getRolesForUser("u$userId"));
    }

    public function getImplicitRolesForUser(int $userId)
    {
        return Casbin::getImplicitRolesForUser("u$userId");
    }

    public function getImplicitRolesForUserForPresentation(int $userId)
    {
        return $this->rolesForPresentation(Casbin::getImplicitRolesForUser("u$userId"));
    }


}
